#ifndef __DDM_H__
#define __DDM_H__

#include <vector>
#include <list.h>

#ifdef DDM_EXPORTS
#define DDM_API __declspec(dllexport)
#else
#define DDM_API __declspec(dllimport)
#endif

namespace DDM
{

enum ERR_TYPE
{
    DBERR_NOERR,
    DBERR_CONN,
    DBERR_STMT,
    DBERR_ENV,
    DBERR_DBC,
    DBERR_NOTOPEN,
    DBERR_SQL,
};

class DDM_API DBCol
{
public:
    DBCol() {len = 0; alloced = 0; data = NULL;}
    unsigned long len;
    int alloced;
    void *data;
};
struct DBRow
{
    std::vector<DBCol*> cols;
};

class DDM_API Database
{
public:
    Database() 
    {
        isConnected = false;
    }
    
    virtual int connect(const char *host, const char *user, const char *passwd, const char *db=NULL) = 0;
    virtual void close(){isConnected = false;}
    
    virtual bool IsConnected() {return isConnected;}

    virtual int execute(const char* sql) = 0;
	virtual int real_execute(const char* sql, std::vector<DBCol*> *cols) = 0;
    virtual int execute_with_result(const char* sql, std::vector<DBRow*> *records) = 0;
    void free_rows(std::vector<DBRow*> *records);

    virtual const char *db_error() const = 0;

    struct list_head list;
    char name[0x40];

protected:
    bool isConnected;
};

extern "C"
{
enum DB_TYPE
{
    DBTYPE_MYSQL,
    DBTYPE_ODBC,
    DBTYPE_SQLITE,
};

DDM_API const char* ddm_version(void);
DDM_API int ddm_version_number(void);

DDM_API Database* GetDB(const char *dbName, DB_TYPE type);
DDM_API void CloseDB(const char *dbName);
}

}
#endif